package com.zrc.superframe_net.room.net

import androidx.room.*

/**
 * @Author: superman
 * @CreateTime: 10/15/21
 * @Describe:
 */
@Dao
interface NetCacheDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun save(vararg netCache: NetCache)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun update(vararg netCache: NetCache): Int

    fun update(url: String, json: String) {
        var bean = find(url)
        if (bean != null) {
            bean.time = System.currentTimeMillis()
            update(bean)
        } else {
            bean = NetCache()
            bean.url = url
            bean.time = System.currentTimeMillis()
            bean.gson = json
            save(bean)
        }
    }

    @Query("SELECT * FROM NetCache WHERE id = :id")
    fun find(id: Long): NetCache?

    @Query("SELECT * FROM NetCache WHERE url = :url")
    fun find(url: String): NetCache?

    @Delete
    fun delete(vararg netCache: NetCache)

    @Query("SELECT * FROM NetCache")
    fun all(): List<NetCache>?
}